import Bun from 'bun';
const glob = new Bun.Glob("*.wav");

import { downloadWav } from './player.js';

const texts = new TextDecoder('utf-8').decode(await Bun.file('text.txt').arrayBuffer());
let i = 0;
let start = false;
for (const text of texts.split(/[\r\n。！？;]/g)) {
    if (text == '>>>START>>>') {
        start = true;
        continue;
    }
    if (!start) continue;
    if (text.length <= 2) continue;
    console.log(i, text);
    await downloadWav(text, `${i}.wav`);
    i++;
    while (Array.from(glob.scanSync()).length > 3) {
        await new Promise(r => setTimeout(r, 200));
    }
}